package com.touchportfolio.view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;
	import com.touchportfolio.VO.NodeVO;
	import com.touchportfolio.model.ApplicationModel;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	

	public class Row extends Sprite
	{

		public var nodeHeight:Number 			= 120;
		public var nodeActiveHeight:Number  	= 155;
		public var nodeOpenHeight:Number 		= 492;
		
		private var _xOffset:Number = 0;
		private var _appModel:ApplicationModel;
		private var _baseColor:Number;
		
		private var _isActive:Boolean;
		
		private var _node:NodeVO;
		
		public function get node():NodeVO
		{
			return _node;
		}
		
		public function set node(value:NodeVO):void
		{
			_node = value;
		}
		
		TweenPlugin.activate([TintPlugin]);

		
		public function Row(node:NodeVO)
		{
			_node = node;
			drawRow();
			
			_appModel = ApplicationModel.getInstance();
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_CHANGE, onActiveRowChange);
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_OPEN, onActiveRowOpen);
			_appModel.addEventListener(ApplicationModel.ACTIVE_ROW_CLOSE, onActiveRowClose);
			
			this.addEventListener(MouseEvent.CLICK, onClickHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			//this.addEventListener(MouseEvent.MOUSE_OUT, onMouseUpHandler);
		}
		
		private function onActiveRowChange(e:Event):void
		{
			if(_appModel.activeRow == _node.rowId)
			{
				TweenLite.to(this,.6,{tint:0x61a479, height:nodeActiveHeight, ease:Elastic.easeOut});
				_isActive = true;
			}
			else if(_isActive)
			{
				TweenLite.to(this,1,{tint:_baseColor, height:nodeHeight, ease:Elastic.easeOut});
			}
		}
		
		private function drawRow():void
		{
			var cell:Sprite;
			
			for(var i:uint=0; i<20; i++)
			{
				cell = new Sprite();
				_baseColor = 0x111111 * _node.rowId;
				cell.graphics.beginFill(_baseColor);
				cell.graphics.drawRect(0, 0, 150, nodeHeight);
				cell.graphics.endFill();
				cell.x = _xOffset;
				_xOffset += cell.width;
				addChild(cell);
			}
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			if(!_appModel.activeRowOpen && _appModel.activeRow == _node.rowId)
				_appModel.activeRowOpen = true;
			else if(_appModel.activeRow == _node.rowId) _appModel.activeRowOpen = false;
		}
		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			_appModel.dragAttract = true;
		}
		
		private function onMouseUpHandler(e:MouseEvent):void
		{
			_appModel.dragAttract = false;
		}
		
		private function onActiveRowOpen(e:Event):void
		{
			if(_appModel.activeRow == _node.rowId)
			{
				TweenLite.to(this,.6,{height:nodeOpenHeight, ease:Elastic.easeOut});
			}
		}
		
		private function onActiveRowClose(e:Event):void
		{
			if(_appModel.activeRow == _node.rowId)
			{
				TweenLite.to(this,1,{height:nodeActiveHeight, ease:Elastic.easeOut});
			}
		}
	}
}